package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.li.work.mapper.SysUserMapper;
import com.ruoyi.teaching.domain.PlatformNNotes;
import com.ruoyi.teaching.domain.UniversalCreateUpdate;
import com.ruoyi.teaching.mapper.PlatformNNotesMapper;
import com.ruoyi.teaching.service.PlatformNNotesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.teaching.utils.UniversalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王旭
 * @since 2024-07-02
 */
@Service
public class PlatformNNotesServiceImpl extends ServiceImpl<PlatformNNotesMapper, PlatformNNotes> implements PlatformNNotesService {

    @Autowired
    private PlatformNNotesMapper platformNNotesMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public AjaxResult selectPlatformNNotesList(Integer partId, Integer courseId, Integer outlineId) {
        int createId = SecurityUtils.getUserId().intValue();
        QueryWrapper<PlatformNNotes> platformNNotesQueryWrapper = new QueryWrapper<>();
        if(courseId != null){
            platformNNotesQueryWrapper.eq("course_id", courseId);
        }else {
            return new AjaxResult(HttpStatus.ERROR,"获取课程失败");
        }
        platformNNotesQueryWrapper.eq("create_id", createId);
        List<PlatformNNotes> platformNNotes = platformNNotesMapper.selectList(platformNNotesQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", platformNNotes);
    }

    @Override
    public AjaxResult selectPlatformNNotesListAll() {
        int createId = SecurityUtils.getUserId().intValue();
        QueryWrapper<PlatformNNotes> platformNNotesQueryWrapper = new QueryWrapper<>();
        platformNNotesQueryWrapper.eq("create_id", createId);
        List<PlatformNNotes> platformNNotes = platformNNotesMapper.selectList(platformNNotesQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", platformNNotes);
    }

    @Override
    public AjaxResult selectPlatformNNotesById(Long id) {
        PlatformNNotes platformNNotes = platformNNotesMapper.selectById(id);
        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", platformNNotes);
    }

    @Override
    public AjaxResult insertPlatformNNotes(PlatformNNotes platformNNotes) {
        UniversalCreateUpdate create = UniversalUtil.getCreateUpdate(sysUserMapper);
        platformNNotes.setCreateBy(create.getName());
        platformNNotes.setCreateTime(DateUtils.getNowDate());
        platformNNotes.setCreateId(create.getId());
        int insert = platformNNotesMapper.insert(platformNNotes);
        if (insert > 0) {
            return new AjaxResult(HttpStatus.SUCCESS, "添加成功");
        } else {
            return new AjaxResult(HttpStatus.ERROR, "添加失败");
        }
    }

    @Override
    public AjaxResult updatePlatformNNotes(PlatformNNotes platformNNotes) {
        UniversalCreateUpdate update = UniversalUtil.getCreateUpdate(sysUserMapper);
        platformNNotes.setCreateBy(update.getName());
        platformNNotes.setCreateTime(DateUtils.getNowDate());
        platformNNotes.setCreateId(update.getId());
        int updatei = platformNNotesMapper.updateById(platformNNotes);
        if (updatei > 0) {
            return new AjaxResult(HttpStatus.SUCCESS, "修改成功");
        } else {
            return new AjaxResult(HttpStatus.ERROR, "修改失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public AjaxResult deletePlatformNNotesByIds(Long[] ids) {
        int length = ids.length;
        int delete = platformNNotesMapper.deleteBatchIds(Arrays.asList(ids));
        if (length == delete) {
            return new AjaxResult(HttpStatus.SUCCESS, "删除成功");
        }
        throw new RuntimeException("删除失败");
    }

    @Override
    public AjaxResult selectPlatformNNotesByProjectId(Integer pageNum, Integer pageSize, Integer projectId) {
        Long userId = null;
        if(null == pageNum){
            pageNum = 1;
        }
        if(null == pageSize){
            pageSize = 10;
        }
        Page<PlatformNNotes> platformNNotesPage = new Page<>(pageNum, pageSize);
        try {
            userId = SecurityUtils.getUserId();
        } catch (ServiceException e) {
            return new AjaxResult(HttpStatus.SUCCESS, "查询成功", platformNNotesPage);
        }
        QueryWrapper<PlatformNNotes> platformNNotesQueryWrapper = new QueryWrapper<>();
        platformNNotesQueryWrapper.eq("course_id", projectId);
        platformNNotesQueryWrapper.eq("create_id", userId);
        platformNNotesPage = platformNNotesMapper.selectPage(platformNNotesPage, platformNNotesQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", platformNNotesPage);
    }
}
